import Vue from 'vue'
import Router from 'vue-router'

import AppIndex from '@/components/pages/AppIndex'
import Login from '../components/Login'
import Home from '../components/layout/Home'

import AssetInfo from '@/components/pages/asset/AssetInfo'
import DepreciationInfo from '@/components/pages/asset/DepreciationInfo'
import HandleInfo from '@/components/pages/asset/HandleInfo'

import PurchaseInfo from '@/components/pages/purchase/PurchaseInfo.vue'
import CategoryInfo from '@/components/pages/purchase/CategoryInfo.vue'
import BrandInfo from '@/components/pages/purchase/BrandInfo.vue'

import UserInfo from '@/components/pages/auth/UserInfo.vue'
import RoleInfo from '@/components/pages/auth/RoleInfo.vue'

import DepartmentInfo from '@/components/pages/system/DepartmentInfo.vue'
import DepositoryInfo from '@/components/pages/system/DepositoryInfo.vue'
import Page404 from '@/components/pages/404.vue'

import AssetCategory from '@/components/charts/AssetCategory.vue'
import AssetDepartment from '@/components/charts/AssetDepartment.vue'
import AssetStatus from '@/components/charts/AssetStatus.vue'

Vue.use(Router)

function route (path, name, component, requireAuth, roles) {
  return {
    path,
    name,
    component,
    meta: {
      requireAuth,
      roles
    }
  }
}

export default new Router({
  mode: 'history', /* 使用 History API 替代 hash */
  routes: [
    {
      path: '/',
      redirect: '/index'
    },
    {
      path: '/home',
      name: 'Home',
      component: Home,
      redirect: '/index/category',
      children: [
        /* 首页 */
        {
          path: '/index',
          name: 'AppIndex',
          component: AppIndex,
          redirect: '/index/category',
          children: [
            route('/index/category', 'AssetCategory', AssetCategory),
            route('/index/department', 'AssetDepartment', AssetDepartment),
            route('/index/status', 'AssetStatus', AssetStatus)
          ],
          meta: {
            requireAuth: true,
            roles: ['ROLE_USER']
          }
        },
        /* 资产管理 */
        route('/asset/info', 'AssetInfo', AssetInfo, true, ['ROLE_USER']),
        route('/asset/depreciation', 'DepreciationInfo', DepreciationInfo, true, ['ROLE_USER']),
        route('/asset/handle', 'HandleInfo', HandleInfo, true, ['ROLE_MANAGE', 'ROLE_APPROVAL', 'ROLE_ADMIN']),
        /* 采购管理 */
        route('/purchase/info', 'PurchaseInfo', PurchaseInfo, true, ['ROLE_USER']),
        route('/purchase/category', 'CategoryInfo', CategoryInfo, true, ['ROLE_PURCHASE', 'ROLE_ADMIN']),
        route('/purchase/brand', 'BrandInfo', BrandInfo, true, ['ROLE_PURCHASE', 'ROLE_ADMIN']),
        /* 权限管理 */
        route('/auth/user', 'UserInfo', UserInfo, true, ['ROLE_ADMIN']),
        route('/auth/role', 'RoleInfo', RoleInfo, true, ['ROLE_ADMIN']),
        /* 系统管理 */
        route('/system/department', 'DepartmentInfo', DepartmentInfo, true, ['ROLE_ADMIN']),
        route('/system/depository', 'DepositoryInfo', DepositoryInfo, true, ['ROLE_ADMIN'])
      ]
    },
    route('/login', 'Login', Login),
    route('/404', '404', Page404),
    {
      path: '*',
      redirect: '/404'
    }
  ]
})
